home *** CD-ROM | disk | FTP | other *** search
/ Shareware Grab Bag / Shareware Grab Bag.iso / 007 / fsizfix.pas < prev    next >
Pascal/Delphi Source File  |  1985-06-03  |  3KB  |  102 lines

  1. {In the PC-DOS and generic MS-DOS implementations of Turbo Pascal version
  2.  3.00B, the function FileSize returns a value which is 1 less than the correct
  3.  value.  An obvious workaround would be simply to add 1 to the value returned
  4.  by FileSize but that generates a secondary problem (demonstrated below) when
  5.  you try to use BlockRead.
  6.  
  7.  This file contains two programs.  The first program demonstrates the above
  8.  mentioned secondary problem.  The second program demonstrates a workaround for
  9.  the problem which will work even when using BlockRead.
  10.  
  11.  At this writing, the first program, which demonstrates the problem, has been
  12.  commented out.  To see the problem demonstrated, delete the line immediately
  13.  below this one.}
  14. (*
  15. program FileSizeError;
  16.  
  17. type
  18.   BufferType = array[1..128] of char;
  19.  
  20. var
  21.   f, t : file;
  22.   ch    : char;
  23.  
  24. procedure open;
  25. begin
  26.   assign(f, 'FILSIZ.FIX');
  27.   reset(f);
  28.   assign(t, 'OUT');
  29.   rewrite(t);
  30. end; { open }
  31.  
  32. procedure CopyFile(blocks : integer);
  33. var
  34.   BlockResults : integer;
  35.   buffer       : BufferType;
  36. begin
  37.   writeln('Copying ', blocks, ' blocks:');
  38.   while blocks > 0 do
  39.   begin
  40.     BlockRead(f, buffer, 1);   { gets I/O error on what should be the last read
  41. }
  42.     Writeln(blocks:5);
  43.     BlockWrite(t, buffer, 1);
  44.     blocks := blocks - 1;
  45.   end;
  46.   close(f);
  47.   close(t);
  48. end; { CopyFile }
  49.  
  50. begin
  51.   writeln('Demonstrating the problem . . .');
  52.   open;
  53.   CopyFile(FileSize(f) + 1);
  54. end.
  55. *)
  56.  
  57.  
  58. { In the following program      DISTRICT OR MSC     - add 1 to the file size when specifying the number of blocks to read
  59.     - add optional paramter "RESULT" to blockread
  60. }
  61.  
  62. program FileSizeFix;
  63.  
  64. type
  65.   BufferType = array[1..128] of char;
  66.  
  67. var
  68.   f, t : file;
  69.   ch    : char;
  70.  
  71. procedure open;
  72. begin
  73.   assign(f, 'FILSIZ.FIX');
  74.   reset(f);
  75.   assign(t, 'OUT');
  76.   rewrite(t);
  77. end; { open }
  78.  
  79. procedure CopyFile(blocks : integer);
  80. var
  81.   BlockResults : integer;
  82.   buffer       : BufferType;
  83. begin
  84.   writeln('Copying ', blocks, ' blocks:');
  85.   while blocks > 0 do
  86.   begin
  87.     BlockRead(f, buffer, 1, BlockResults);   { with new paramter }
  88.     Writeln(blocks:5, BlockResults:5);
  89.     BlockWrite(t, buffer, 1);
  90.     blocks := blocks - 1;
  91.   end;
  92.   close(f);
  93.   close(t);
  94. end; { CopyFile }
  95.  
  96. begin
  97.   writeln('Demonstrating the solution . . .');
  98.   open;
  99.   CopyFile(FileSize(f) + 1 );                 { add one more block }
  100. end.
  101. d.
  102.